export default {
  data() {
    return {
      isDataLoad: false,
      tableHeadData: [],
      tableData: [],
      tableConfig: {
        interfacePath: '',
        hideFuncBar: true
      }
    }
  },
  methods: {
    async initTableData() {
      const keys = Object.keys(this.formData)
      let flag = true // 是否允许查询
      keys.forEach(k => {
        if (!(this.noRequirdSearch && this.noRequirdSearch.indexOf(k) > -1)) {
          if ([null, undefined, ''].includes(this.formData[k])) {
            flag = false
          }
        }
      })
      if (!flag) return

      this.isDataLoad = true
      await this.queryTableData()
      this.isDataLoad = false
    },
    /**
     * 重构表格数据
     * @param {object} data 接口返回的原数据
     * @param {string} firstColumnField 表格第一列的字段name
     * */
    resetTableData(data, firstColumnField) {
      this.tableHeadData = data['月份']
      const rowList = []
      for (const rowIndex in data) {
        // 重置表格行数据 (除月份以外的其他行)
        if (rowIndex !== '月份') {
          const obj = {}
          const row = data[rowIndex]
          this.tableHeadData.forEach((prop, columnIndex) => {
            // 遍历表头，重置表格列数据
            obj[prop] = row[columnIndex]
          })
          rowList.push(obj)
        }
      }
      // 同比趋势与环比趋势作为判断环比与同比指标状态：1 上升， -1 下降
      const bodyList = rowList.filter(v => !['同比趋势', '环比趋势', '同比'].includes(v['销量']))// 取消同比显示
      const statusList = rowList.filter(v => ['同比趋势', '环比趋势'].includes(v['销量']))

      this.tableData = bodyList.map(row => {
        const label = row[firstColumnField]
        const label2 = row['销量']
        if (label2 === '同比' || label2 === '环比') {
          const status = statusList.find(v => v[firstColumnField] === label && v['销量'] === label2 + '趋势')
          return {
            ...row, ...{ status }
          }
        }
        return row
      })
    },
    // 表格合并行
    tableSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
